*AmericasBarometer cleaning

clear
use "$data_dir/raw/Grand_Merge_2004-2023_LAPOP_AmericasBarometer_v1.0_w.dta"

label language en

decode pais, gen(country)
replace country = "Venezuela" if pais==16
replace country = "Guyana" if pais==24

merge m:1 country using "$data_dir/raw/countrycodes.dta"
drop if _merge==2
drop _merge

gen survey = "AmericasBarometer"

gen weight = wt

**********************************
*************TRUST VALUES*********
**********************************

//trust is always referred to as above the median, and no trust if less than or equal to the median

summ b14, de
gen trust_govt = 1 if inlist(b14, 5, 6, 7) | inlist(anestg, 1, 2)
replace trust_govt = 0 if inlist(b14, 1, 2, 3, 4) | inlist(anestg, 3, 4)

summ b10a, de
gen trust_justice = 1 if inlist(b10a, 5, 6, 7)
replace trust_justice = 0 if inlist(b10a, 1, 2, 3, 4)

summ b18, de
gen trust_police = 1 if inlist(b18, 5, 6, 7)
replace trust_police = 0 if inlist(b18, 1, 2, 3, 4)

gen trust_civil_service = .

summ b32, de
gen trust_local_govt = 1 if inlist(b32, 5, 6, 7)
replace trust_local_govt = 0 if inlist(b32, 1, 2, 3, 4)

summ b21, de
gen trust_parties = 1 if inlist(b21, 4, 5, 6, 7)
replace trust_parties = 0 if inlist(b21, 1, 2, 3, 4)

summ b13, de
gen trust_parliament = 1 if inlist(b13, 5, 6, 7)
replace trust_parliament = 0 if inlist(b13, 1, 2, 3, 4)

gen trust_others = .

summ b12, de
gen trust_military = 1 if inlist(b12, 5, 6, 7)
replace trust_military = 0 if inlist(b12, 1, 2, 3, 4)

pca trust_govt trust_justice trust_police trust_local_govt trust_parties trust_military, comp(1)
predict mtrust_state
summ mtrust_state, de
gen trust_state = 1 if mtrust_state>=r(p50) & mtrust_state!=.
replace trust_state = 0 if mtrust_state<r(p50) & mtrust_state!=.

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

clonevar survey_year = year
clonevar age = q2

gen yob = survey_year-age

gen cohort = ""
forvalues i = 1870(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

rename sex sex_old
gen sex = 1 if sex_old==1
replace sex = 2 if sex_old==2
replace sex = -99 if sex_old==.

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*			7   Hindu 
*		  -99   Missing 

gen religion = .
replace religion = 1 if inlist(q3c, 4, 11)
replace religion = 3 if inlist(q3c, 10)
replace religion = 4 if inlist(q3c, 1, 2, 5, 6, 12, 4114)
replace religion = 5 if inlist(q3c, 2701, 4113)
replace religion = 6 if inlist(q3c, 77, 1501, 4115)
replace religion = 7 if inlist(q3c, 2702)
replace religion = -99 if q3c==.
replace religion = -99 if religion==.

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

summ ed if edr==0
summ ed if edr==1
summ ed if edr==2
summ ed if edr==3

//create the categories based on at least completion (i.e. we can comfortably guess primary school was completed if years of education aer greater than or equal to 5, the mean)
gen edu = .
replace edu = 1 if ed>=5 & ed<10 //5 years means completed primary but less than 10 years still not completed secondary
replace edu = 2 if ed>=10 & ed<=14 //10 years means completed secondary but less than 14 years still not completed tertiary
replace edu = 3 if ed>=15 & ed!=. //15 years or more means completed tertiary
replace edu = 4 if ed<5 & ed!=.
replace edu = -99 if edu==.

*occupation
*			1   Managers
*			2   Professionals
*			3   Technicians and Associate Professionals
*			4   Clerical Support Workers
*			5   Service and Sales Workers
*			6   Skilled Agricultural, Forestry and Fishery Workers
*			7   Craft and Related Trades Workers
*			8   Plant and Machine Operators, and Assemblers
*			9   Elementary Occupations
*			0   Armed Forces Occupations
*	      -99   Missing

gen occupation = -99

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = 1 if inlist(ocup4a, 1, 2)
replace employment_status = 2 if inlist(ocup4a, 3)
replace employment_status = 3 if inlist(ocup4a, 4, 5, 6, 7)
replace employment_status = -99 if employment_status==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*			6   Band 6
*			7   Band 7
*			8   Band 8
*			9   Band 9
*		   10   Band 10
*		  -99   Missing

gen income = -99

*ideology
*			1   left  
*			2         
*			3         
*			4         
*			5         
*			6         
*			7         
*			8         
*			9         
*			10  right
*		   -99  Missing

rename ideology ideology_old
gen ideology = -99

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if inlist(q11n, 2, 3)
replace marital = 2 if inlist(q11n, 1, 4, 5, 6, 7)
replace marital = -99 if marital==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if ur==1
replace urban = 2 if ur==2
replace urban = -99 if urban==.

*born_country
*			1   Born in country
*			0   Not born in country

gen born_country = -99

keep country ccode numcode country_short wave S009 cow survey weight trust_govt trust_justice trust_police trust_civil_service trust_local_govt trust_parties trust_parliament trust_others trust_military trust_state survey_year age yob cohort sex religion edu occupation employment_status income ideology marital urban born_country

sort ccode wave survey_year

save "$data_dir/clean/americasbarometer_clean.dta", replace

